package drds.configuration.db.xml_mapping.configuration;


import drds.configuration.db.xml_mapping.MapperFactory;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * Mapper分为参数字段/sql/显示结果字段
 */
@Slf4j
public class ParameterMapAndResultMapAssistant {
    @Getter
    private final String mapperXmlId;
    @Setter
    @Getter
    public Configuration configuration;

    public ParameterMapAndResultMapAssistant(Configuration configuration, String mapperXmlId) {
        this.configuration = configuration;
        this.mapperXmlId = mapperXmlId;
    }

    //
    public ParameterMap addParameterMap(String id, Class<?> parameterClass, List<ParameterMapping> parameterMappingList) {
        List<ParameterMapping> extendedParameterMappingList = new ArrayList<ParameterMapping>();
        String extend = parameterClass.getName();
        if (extend != null) {
            if (!MapperFactory.DtoClassMapping.resultMapIdToResultMapMap.containsKey(extend)) {
                throw new RuntimeException("Could not find a parent resultmap with id '" + extend + "'");
            }
            ParameterMap parameterMap = MapperFactory.DtoClassMapping.parameterMapIdToParameterMapMap.get(extend);
            extendedParameterMappingList.addAll(parameterMap.copyParameterMappingList());
        }
        //
        for (ParameterMapping extendedParameterMapping : extendedParameterMappingList) {
            boolean exists = false;
            for (ParameterMapping parameterMapping : parameterMappingList) {
                if (extendedParameterMapping.getPropertyName().equals(parameterMapping.getPropertyName())) {
                    exists = true;
                    break;
                }
            }
            //局部纠正,不存在则加进来
            if (!exists) {
                parameterMappingList.add(extendedParameterMapping);
            }
        }
        //必须加在配置中
        ParameterMap parameterMap = new ParameterMap();
        parameterMap.setId(id);
        parameterMap.setParameterClass(parameterClass);
        parameterMap.setParameterMappingList(parameterMappingList);
        //
        for (ParameterMapping parameterMapping : parameterMap.getParameterMappingList()) {
            final String propertyName = parameterMapping.getPropertyName();
            final String columnName = parameterMapping.getColumnName().toLowerCase();
            parameterMap.getPropertyNameSet().add(propertyName);
            parameterMap.getColumnNameSet().add(columnName);
            parameterMap.getPropertyNameToColumnNameMap().put(propertyName, columnName);
            parameterMap.getColumnNameMapToPropertyNameMap().put(columnName, propertyName);
        }
        //
        parameterMap.setParameterMappingList(parameterMappingList);
        configuration.addParameterMap(parameterMap);
        return parameterMap;
    }

    public ParameterMapping buildParameterMapping(String propertyName, String columnName) {
        ParameterMapping parameterMapping = new ParameterMapping();
        parameterMapping.setPropertyName(propertyName);
        parameterMapping.setColumnName(columnName);
        return parameterMapping;
    }

    public ParameterMap getParameterMap(String parameterMapId) {
        return configuration.getParameterMap(parameterMapId);
    }

    //
    public ResultMap addResultMap(//
                                  String id,//
                                  Class<?> resultMapClass,//
                                  List<ResultMapping> resultMappingList) {
        List<ResultMapping> extendedResultMappingList = new ArrayList<ResultMapping>();

        String extend = resultMapClass.getName();
        if (extend != null) {
            if (!MapperFactory.DtoClassMapping.resultMapIdToResultMapMap.containsKey(extend)) {
                throw new RuntimeException("Could not find a parent resultmap with id '" + extend + "'");
            }
            ResultMap resultMap = MapperFactory.DtoClassMapping.resultMapIdToResultMapMap.get(extend);
            extendedResultMappingList.addAll(resultMap.copyResultMappingList());
        }
        //
        for (ResultMapping extendedResultMapping : extendedResultMappingList) {
            boolean exists = false;
            for (ResultMapping resultMapping : resultMappingList) {
                if (extendedResultMapping.getPropertyName().equals(resultMapping.getPropertyName())) {
                    exists = true;
                    break;
                }
            }
            //局部纠正,不存在则加进来
            if (!exists) {
                resultMappingList.add(extendedResultMapping);
            }
        }
        ResultMap resultMap = new ResultMap();
        resultMap.setResultMapClass(resultMapClass);
        resultMap.setResultMappingList(resultMappingList);
        for (ResultMapping resultMapping : resultMap.getResultMappingList()) {
            final String propertyName = resultMapping.getPropertyName();
            final String columnName = resultMapping.getColumnName().toLowerCase();
            resultMap.getPropertyNameSet().add(propertyName);
            resultMap.getColumnNameSet().add(columnName);
            resultMap.getPropertyNameToColumnNameMap().put(propertyName, columnName);
            resultMap.getColumnNameMapToPropertyNameMap().put(columnName, propertyName);
        }
        //必须加在配置中
        configuration.addResultMap(id, resultMap);
        return resultMap;
    }

    public ResultMap getResultMap(String resultMapId) {
        return configuration.getResultMap(resultMapId);
    }

    //

    public ResultMapping buildResultMapping(//
                                            String columnPrefix,//
                                            String propertyName,//
                                            String columnName) {//
        ResultMapping resultMapping = new ResultMapping();
        resultMapping.setColumnPrefix(columnPrefix);
        resultMapping.setPropertyName(propertyName);
        resultMapping.setColumnName(columnName);
        return resultMapping;
    }


}
